package com.coscon.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;



/**
 * 获取客户端IP
 * @author 409390047@qq.com
 * @Date: 2021/6/16 10:13
 */

public class ClientIpUtil {




    /**
     * 根据request获取IP
     * @param request
     * @return
     */
    public static String getRemoteAddr(HttpServletRequest request) {
        //特殊处理下,因为经过apache http server 代理
        String remoteAdress = request.getRemoteAddr();
        String xRealIp = request.getHeader("X-Real-IP");
        String xForwardedFor = request.getHeader("X-Forwarded-For");
        //两者都为空的情况下，则获取request.getRemoteAddr();
        if(StringUtils.isBlank(xRealIp) && StringUtils.isBlank(xForwardedFor)){
            return remoteAdress;
        }
        //如果两个相同，随便返回一个，如果不同
        if(StringUtils.equalsIgnoreCase(xRealIp, xForwardedFor)){
            remoteAdress = xRealIp;
            return remoteAdress;
        }
        //如果X_Forwarded_For 为空，X_Real_IP不为空，则返回X_Real_IP值
        if(StringUtils.isBlank(xForwardedFor) && StringUtils.isNotBlank(xRealIp) ){//如果X_Forwarded_For 为空
            remoteAdress = xRealIp;
            return remoteAdress;
        }
        //如果X_Forwarded_For不为空，并且有逗号的，则获取第一个
        if(StringUtils.isNotBlank(xForwardedFor) && StringUtils.contains(xForwardedFor, ",") ){//如果X_Forwarded_For不为空
            remoteAdress = StringUtils.split(xForwardedFor, ",")[0];
            return remoteAdress;
        }
        //如果X_Forwarded_For不为空，并且，没有包含逗号的。则直接返回X_Forwarded_For值
        if(StringUtils.isNotBlank(xForwardedFor) && !StringUtils.contains(xForwardedFor, ",") ){
            remoteAdress = xForwardedFor;
            return remoteAdress;
        }
        remoteAdress = request.getHeader("X-Real-IP");
        if (StringUtils.isEmpty(remoteAdress)
                || StringUtils.equalsIgnoreCase(remoteAdress, "unknown")) {
            remoteAdress = request.getHeader("X-Forwarded-For");
        }
        if (StringUtils.isEmpty(remoteAdress)
                || StringUtils.equalsIgnoreCase(remoteAdress, "unknown")) {
            remoteAdress = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isEmpty(remoteAdress)
                || StringUtils.equalsIgnoreCase(remoteAdress, "unknown")) {
            remoteAdress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isEmpty(remoteAdress)
                || StringUtils.equalsIgnoreCase(remoteAdress, "unknown")) {
            remoteAdress = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtils.isEmpty(remoteAdress)
                || StringUtils.equalsIgnoreCase(remoteAdress, "unknown")) {
            remoteAdress = request.getRemoteAddr();
        }
        if (StringUtils.isNotEmpty(remoteAdress)
                || !StringUtils.equalsIgnoreCase(remoteAdress, "unknown")) {
            remoteAdress = StringUtils.split(remoteAdress, ",")[0];
        }
        return remoteAdress;
    }

}
